window.onload = function(){
    getAll()
}
function getAll(){
    let xhr = new XMLHttpRequest()
    /* 
        https://www.bingjs.com:8001/Student/GetAll    查询全部学生   [GET请求]
    */
    xhr.open("GET",`https://www.bingjs.com:8001/Student/GetAll`)
    xhr.send()
    xhr.onreadystatechange = function(){
        if(xhr.readyState === 4 && xhr.status === 200){
            let str = JSON.parse(xhr.response)
            console.log(str)
            str.forEach(a=>{
                let tr = document.createElement("tr")
                // 学号
                let td_studentNo = document.createElement("td")
                td_studentNo.innerHTML = a.StudentNo
                // 密码
                let td_studentLoginPwd = document.createElement("td")
                td_studentLoginPwd.innerHTML = a.LoginPwd
                // 姓名
                let td_studentName = document.createElement("td")
                td_studentName.innerHTML = a.StudentName
                // 性别
                let td_studentSex = document.createElement("td")
                td_studentSex.innerHTML = a.Sex === "M"?"男":"女"
                // 年级
                let td_studentGrade = document.createElement("td")
                td_studentGrade.innerHTML = a.Grade.GradeName
                // 电话
                let td_studentPhone = document.createElement("td")
                td_studentPhone.innerHTML = a.Phone
                // 地址
                let td_studentAddress = document.createElement("td")
                td_studentAddress.innerHTML = a.Address
                // 出生日期
                let td_studentBorn = document.createElement("td")
                td_studentBorn.innerHTML = bornDate(new Date(a.BornDate))
                // 邮箱
                let td_studentEmail = document.createElement("td")
                td_studentEmail.innerHTML = a.Email
                // 身份证
                let td_studentId = document.createElement("td")
                td_studentId.innerHTML = a.IdentityCard

                tr.appendChild(td_studentNo)
                tr.appendChild(td_studentLoginPwd)
                tr.appendChild(td_studentName)
                tr.appendChild(td_studentSex)
                tr.appendChild(td_studentGrade)
                tr.appendChild(td_studentPhone)
                tr.appendChild(td_studentAddress)
                tr.appendChild(td_studentBorn)
                tr.appendChild(td_studentEmail)
                tr.appendChild(td_studentId)
                tbody.appendChild(tr)
            })
        }
    }
}


//修改日期格式
function bornDate(data) {
    let year = data.getFullYear()
    let month = data.getMonth() + 1
    let day = data.getDate()
    // console.log(year, month, day);
    return [year, month, day].map(a => pacth0(a)).join("-")

}

// 补0操作 如果是单数就添加一个0
function pacth0(val) {
    return (val + "").length > 1 ? val : "0" + val
}